package eproject.mm.com.projectsupervision.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;

public class ProblemRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mLayoutInflater;
    private Context context;
    private List<Map> lists;

    //建立枚举 2个item 类型
    public enum ITEM_TYPE {
        PLAN,//进度报告
        PROBLEM //问题上传
    }

    public ProblemRecyclerViewAdapter(Context context, List<Map> lists) {
        this.lists = lists;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemProblemViewHolder(mLayoutInflater.inflate(R.layout.rv_item_problem, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        Map item = (Map) lists.get(position);
        if (holder instanceof ItemProblemViewHolder) {
            ((ItemProblemViewHolder) holder).tv_person.setText((String) item.get("supunitname") + " " + (String) item.get("creatorname"));
            String sTime = (String) item.get("createtime");
            if (sTime.length() > 16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemProblemViewHolder) holder).tv_time.setText(sTime);
            ((ItemProblemViewHolder) holder).tv_text.setText((String) item.get("describes"));
            RecyclerView rv_images = ((ItemProblemViewHolder) holder).rv_images;

            String type = (String) item.get("type") + "";
            String status = (String) item.get("status") + "";
            if (type.equals("0")) {
                if(status.equals("3")) {
                    ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context, R.color.colorProblemFixed));
                }else{
                    ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context, R.color.colorProblem));
                }
            } else {
                ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context, R.color.colorProblem));
            }

            String sImagesStr = (String) item.get("newnames2");
            String bImagesStr = (String) item.get("newnames");
            if (sImagesStr != null && !sImagesStr.trim().equals("")) {
                String[] sImagesArray = sImagesStr.split(",");
                String[] bImagesArray = bImagesStr.split(",");

                if (sImagesArray != null) {
                    List<String> sImages = new ArrayList<>();
                    List<String> bImages = new ArrayList<>();
                    for (int i = 0; i < sImagesArray.length; i++) {
                        sImages.add(sImagesArray[i]);

                        bImages.add("http://" + PrjApplication.getInstance().getServerIp() + ":" + PrjApplication.getInstance().getServerPort() + bImagesArray[i]);
                    }
                    BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(context, sImages, bImages);
                    ((ItemProblemViewHolder) holder).rv_images.setLayoutManager(new GridLayoutManager(context, 4));
                    rv_images.getLayoutManager().setAutoMeasureEnabled(true);
                    ((ItemProblemViewHolder) holder).rv_images.setAdapter(imageViewAdapter);
                    ((ItemProblemViewHolder) holder).rv_images.setVisibility(View.VISIBLE);
                }
            } else {
                ((ItemProblemViewHolder) holder).rv_images.setVisibility(View.GONE);
            }

            ((ItemProblemViewHolder) holder).tv_projectName.setText((String) item.get("projectname"));
            ((ItemProblemViewHolder) holder).tv_buildUnitName.setText((String) item.get("buildunitname"));
            ((ItemProblemViewHolder) holder).tv_planNodeName.setText((String) item.get("taskname"));


            if(mOnItemClickListener!=null){
                ((ItemProblemViewHolder) holder).rl_problem_item.setOnLongClickListener(new View.OnLongClickListener(){
                    @Override
                    public boolean onLongClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                        return true;
                    }
                });
                ((ItemProblemViewHolder) holder).rl_problem_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }

        }
    }


    @Override
    public int getItemViewType(int position) {
        Map item = (Map) lists.get(position);
        return ITEM_TYPE.PROBLEM.ordinal();
    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    public static class ItemProblemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_person, tv_time, tv_text, tv_projectName, tv_buildUnitName, tv_planNodeName;
        IconFontTextview iftv_icon;
        RecyclerView rv_images;
        RelativeLayout rl_problem_item;

        public ItemProblemViewHolder(View itemView) {
            super(itemView);
            iftv_icon = (IconFontTextview) itemView.findViewById(R.id.iftv_icon);
            tv_person = (TextView) itemView.findViewById(R.id.tv_person);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_text = (TextView) itemView.findViewById(R.id.tv_text);
            tv_projectName = (TextView) itemView.findViewById(R.id.tv_projectName);
            tv_buildUnitName = (TextView) itemView.findViewById(R.id.tv_buildUnitName);
            tv_planNodeName = (TextView) itemView.findViewById(R.id.tv_planNodeName);

            rv_images = (RecyclerView) itemView.findViewById(R.id.rv_images);
            rl_problem_item = (RelativeLayout) itemView.findViewById(R.id.rl_problem_item);
        }
    }

    public void addItems(List list) {
        int pos = lists.size();
        lists.addAll(pos, list);

        notifyItemInserted(pos);
    }

    public void setItems(List list) {
        lists = list;

        notifyDataSetChanged();
    }
    public Object getItemAt(int pos){
        return lists.get(pos);
    }
    onItemClickListener mOnItemClickListener;

    public interface onItemClickListener {
        public void onItemClick(View view, int position);

        public void onItemLongClick(View view, int position);

    }

    public void setOnItemClickLitsener(onItemClickListener mOnItemClickLitsener) {
        mOnItemClickListener = mOnItemClickLitsener;
    }
}